<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.staticfile.org/vue/2.6.11/vue.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/iview/3.5.4/styles/iview.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/iview/3.5.4/iview.min.js"></script>
    <title>后台管理</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        .preloader {
            position: fixed;
            z-index: -1;
            top: 0;
            left: 0;
            opacity: 0.5;
        }

        .header {
            height: 56px;
            border-bottom: 1px solid #e9e9e9;
            line-height: 56px;
            background-color: #fff;
            box-shadow: 0 0 5px #d1d1d1;
        }

        .header > h3 {
            margin-left: 20px;
        }

        .main {
            width: 380px;
            margin: 90px auto 0;
            background: #fff;
            border-radius: 4px;
            border: 1px solid #d8d8d8;
            padding: 20px;
            box-shadow: 0 0 5px #d1d1d1;
        }

        .main .ivu-form-item-content {
            display: flex;
            justify-content: center;
        }

        .main .ivu-tabs-nav {
            width: 100%;
        }

        .main .ivu-tabs-nav .ivu-tabs-tab {
            width: 50%;
            text-align: center;
            margin: 0;
        }
    </style>
</head>
<body>
<div class="preloader">
    <canvas id="canvas"></canvas>
</div>
<div id="app">
    <header class="header">
        <h3>老年人心理健康评估</h3>
    </header>
    <div class="main">
        <tabs value="name1">
            <tab-pane label="登 录" name="name1">
                <i-form :model="login_form" style="margin-top: 50px;" :rules="ruleValidate">
                    <form-item prop="email">
                        <i-input type="text" v-model="login_form.email" placeholder="邮箱" size="large">
                            <icon type="ios-at-outline" slot="prepend"></icon>
                        </i-input>
                    </form-item>
                    <form-item>
                        <i-input type="password" v-model="login_form.pwd" placeholder="密码" size="large">
                            <icon type="ios-lock-outline" slot="prepend"></icon>
                        </i-input>
                    </form-item>
                    <form-item>
                        <div style="display: flex;flex-direction: column;">
                            <i-button type="primary" size="large" @click="login">登 录</i-button>
                            <i-button @click="forget" size="small" style="margin-top: 30px">忘记密码</i-button>
                        </div>
                    </form-item>
                </i-form>
            </tab-pane>
            <tab-pane label="注 册" name="name2">
                <i-form :model="reg_form" :rules="ruleValidate">
                    <form-item>
                        <i-input type="text" v-model="reg_form.name" placeholder="用户名" size="large">
                            <icon type="ios-person-outline" slot="prepend"></icon>
                        </i-input>
                    </form-item>
                    <form-item prop="email">
                        <i-input type="text" v-model="reg_form.email" placeholder="邮箱" size="large">
                            <icon type="ios-at-outline" slot="prepend"></icon>
                        </i-input>
                    </form-item>
                    <form-item>
                        <i-input type="text" v-model="reg_form.code" placeholder="验证码" size="large">
                            <icon type="ios-at-outline" slot="prepend"></icon>
                        </i-input>
                        <i-button @click="get_auth_code" style="min-width: 90px;">[[
                            auth_code.state?'获取验证码':auth_code.count_down ]]
                        </i-button>
                    </form-item>
                    <form-item>
                        <i-input type="password" v-model="reg_form.pwd1" placeholder="密码" size="large">
                            <icon type="ios-lock-outline" slot="prepend"></icon>
                        </i-input>
                    </form-item>
                    <form-item>
                        <i-input type="password" v-model="reg_form.pwd2" placeholder="确认密码" size="large">
                            <icon type="ios-lock-outline" slot="prepend"></icon>
                        </i-input>
                    </form-item>
                    <form-item>
                        <i-button type="primary" size="large" @click="register">注 册</i-button>
                    </form-item>
                </i-form>
            </tab-pane>
        </tabs>
    </div>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<script src="{{ url_for('static',filename='js/particle-alone.js') }}"></script>
<script>
    var vm = new Vue({
        delimiters: ['[[', ']]'],
        el: '#app',
        data: {
            login_form: {
                email: "",
                pwd: ""
            },
            reg_form: {
                name: "1",
                code: "1",
                email: "2717768255@qq.com",
                pwd1: "123",
                pwd2: "123"
            },
            auth_code: {
                state: true,
                count_down: 0,
                interval: NaN
            },
            ruleValidate: {
                email: [
                    {required: true, message: '邮箱不能为空！', trigger: 'blur'},
                    {type: 'email', message: '邮箱格式不正确！', trigger: 'blur'}
                ]
            }
        },
        mounted: function () {

        },
        methods: {
            {#获取验证码#}
            get_auth_code() {
                if (this.auth_code.state) {
                    if (this.reg_form.email && /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/.test(this.reg_form.email)) {
                        let params = new FormData();
                        params.append('email', this.reg_form.email)
                        params.append('action', 'auth_code')
                        this.$Loading.start();
                        this.auth_code.count_down = 30
                        this.auth_code.state = false
                        this.auth_code.internal = setInterval(() => {
                            this.auth_code.count_down -= 1
                            if (this.auth_code.count_down === 0) {
                                clearInterval(this.auth_code.internal)
                                this.auth_code.state = true
                            }
                        }, 1000)
                        axios.post('', params).then(res => {
                            if (res.data.state) {
                                swal({
                                    title: '发送成功',
                                    icon: "success",
                                    timer: 800
                                }).then(() => {
                                    this.$Loading.finish();
                                })
                            } else {
                                swal({
                                    title: '发送失败',
                                    icon: "error",
                                }).then(() => {
                                    this.$Loading.error();
                                })
                            }
                        })
                    } else {
                        this.$Message['warning']({
                            background: true,
                            content: '邮箱错误！'
                        });
                    }
                }
            },
            {#登录#}
            login() {
                if (/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/.test(this.login_form.email)) {
                    if (this.login_form.pwd) {
                        let param = new FormData();
                        param.append('email', this.login_form.email);
                        param.append('pwd', this.login_form.pwd);
                        param.append('action', 'login');
                        axios.post('', param).then(res => {
                            console.log(res.data);
                            if (res.data.state) {
                                swal({
                                    title: '登陆成功',
                                    icon: "success",
                                    timer: 800
                                }).then(() => {
                                    Cookies.set('uid', res.data.uid);
                                    window.location.href = '/'
                                })
                            } else {
                                swal('登陆失败', res.data.msg, "error")
                            }
                        })
                    } else {
                        this.$Message['warning']({
                            background: true,
                            content: '请填写密码！'
                        });
                    }
                } else {
                    this.$Message['warning']({
                        background: true,
                        content: '邮箱格式错误！'
                    });
                }

            },
            {#注册#}
            register() {
                if (this.reg_form.name && this.reg_form.email && this.reg_form.pwd1 && this.reg_form.pwd2) {
                    if (/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/.test(this.reg_form.email)) {
                        if (this.reg_form.pwd1 === this.reg_form.pwd2) {
                            let params = new FormData();
                            params.append('name', this.reg_form.name)
                            params.append('pwd', this.reg_form.pwd1)
                            params.append('email', this.reg_form.email)
                            params.append('code', this.reg_form.code)
                            params.append('action', 'register')
                            axios.post('', params).then(res => {
                                if (res.data.state) {
                                    swal({
                                        title: '注册成功',
                                        icon: "success",
                                        timer: 800
                                    }).then(() => {
                                        location.reload()
                                    })
                                } else {
                                    swal('注册失败', res.data.msg, "error")
                                }
                            })
                        } else {
                            this.$Message['warning']({
                                background: true,
                                content: '密码不一致！'
                            });
                        }
                    } else {
                        this.$Message['warning']({
                            background: true,
                            content: '邮箱格式错误！'
                        });
                    }
                } else {
                    this.$Message['warning']({
                        background: true,
                        content: '请填写完整！！'
                    });
                }
            },
            {#忘记密码#}
            forget() {
                if (/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/.test(this.login_form.email)) {
                    this.$Loading.start();
                    let param = new FormData();
                    param.append('email', this.login_form.email);
                    param.append('action', 'forget');
                    axios.post('', param).then(res => {
                        console.log(res.data);
                        if (res.data.state) {
                            this.$Loading.finish();
                            swal({
                                title: '获取成功',
                                text: '密码已发送至你的邮箱，请注意查收',
                                icon: "success",
                                timer: 800
                            })
                        } else {
                            swal('获取失败', res.data.msg, "error")
                        }
                    })
                } else {
                    this.$Loading.error();
                    this.$Message['warning']({
                        background: true,
                        content: '邮箱格式错误！'
                    });
                }
            }
        }
    });
</script>
</html>
